#include <EEPROM.h>

#define key_A25   10     //频率1，A25
#define key_A26   5      //频率2, A26
#define key_A27   A5     //频率3, A27 

#define RANDOMMM 8        //点击随机时间幅度

float hong = 4.4;         //频率1，A25低电平触发
float bai = 8.65;         //频率2，A26低电平触发
float pv3 = 10.2;         //频率3，A27低电平触发

int minTime1 = int(500/hong) - RANDOMMM;  //最小随机间隔时间
int maxTime1 = int(500/hong) + RANDOMMM;  //最大随机间隔时间

int minTime2 = int(500/bai) - RANDOMMM;  //最小随机间隔时间
int maxTime2 = int(500/bai) + RANDOMMM;  //最大随机间隔时间

int minTime3 = int(500/pv3) - RANDOMMM;  //最小随机间隔时间
int maxTime3 = int(500/pv3) + RANDOMMM;  //最大随机间隔时间

//红色按住时间随机最大值和最小值75.4-->1000/75.4=14
#define COUNT_1 5
int minTTime_1 = 117;
int maxTTime_1 = 84;

//白色按住时间随机最大值和最小值68--->1000/68=15
#define COUNT_2 11
int minTTime_2 = 67;
int maxTTime_2 = 61;

//第三频率按住时间随机最大值和最小值68--->1000/68=15,98.03
#define COUNT_3 15
int minTTime_3 = 57;
int maxTTime_3 = 51;

const int tPin[2] = {A2,A1};  //1号引脚定义,灰的在前，白的在后

void touchPin(){
    digitalWrite(tPin[1], HIGH);
    digitalWrite(tPin[0], LOW);  
}
void unTouchPin(){
    digitalWrite(tPin[0], HIGH);
    digitalWrite(tPin[1], LOW);  
}

const int blinkdelay = 300;

void ledBlink(){
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(blinkdelay);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(blinkdelay); 
}

void setup() {
  //初始化串口
  Serial.begin(115200);

  //设置随机数种子
  randomSeed(analogRead(A0));
  pinMode(key_A25, INPUT_PULLUP); //设置按键引脚为输入
  pinMode(key_A26, INPUT_PULLUP); //设置按键引脚为输入
  pinMode(key_A27, INPUT_PULLUP);

  pinMode(LED_BUILTIN, OUTPUT);
  //设置点击头控制引脚为输出
    pinMode(tPin[0], OUTPUT);
    pinMode(tPin[1], OUTPUT);
  delay(1);
  //初始化点击头状态,清除所有当关点击状态
  unTouchPin();
  digitalWrite(LED_BUILTIN, LOW);

}

int ttime = 125;
int utime = 125;

int8_t count1 = COUNT_1;
int8_t count2 = COUNT_2;
int8_t count3 = COUNT_3;

void loop() {

  //混合频率1+2
  int k1 = digitalRead(key_A25);  //频率1
  int k2 = digitalRead(key_A26);  //频率2
  int k3 = digitalRead(key_A27);  //频率3

  if(!k1 && !k2 && k3){ //频率1+2
          if(count1 >= 0){
             //红色
              ttime = random(minTTime_1,maxTTime_1);   //取随机时间
              touchPin();
              delay(ttime);
              utime = random(minTime1,maxTime1);   //取随机时间
              unTouchPin();
              delay(utime);   
              count1--;
          }else{
              if(count2 >=0){
                    ttime = random(minTTime_2,maxTTime_2);   //取随机时间
                    touchPin();
                    delay(ttime);
                    utime = random(minTime2,maxTime2);   //取随机时间
                    unTouchPin();
                    delay(utime);    
                    count2--;
              }else{
                count1 = COUNT_1;
                count2 = COUNT_2;
              }
          }     
  }else if(!k1 && k2 && !k3){//1+3
    if(count1 >= 0){
             //红色
              ttime = random(minTTime_1,maxTTime_1);   //取随机时间
              touchPin();
              delay(ttime);
              utime = random(minTime1,maxTime1);   //取随机时间
              unTouchPin();
              delay(utime);   
              count1--;
          }else{
              if(count3 >=0){
                    ttime = random(minTTime_3,maxTTime_3);   //取随机时间
                    touchPin();
                    delay(ttime);
                    utime = random(minTime3,maxTime3);   //取随机时间
                    unTouchPin();
                    delay(utime);    
                    count3--;
              }else{
                count1 = COUNT_1;
                count3 = COUNT_3;
              }
          }   
  }else if(!k1 && !k2 && !k3){//1+2+3
    if(count1 >= 0){
             //红色
              ttime = random(minTTime_1,maxTTime_1);   //取随机时间
              touchPin();
              delay(ttime);
              utime = random(minTime1,maxTime1);   //取随机时间
              unTouchPin();
              delay(utime);   
              count1--;
          }else{
              if(count2 >=0){
                    ttime = random(minTTime_2,maxTTime_2);   //取随机时间
                    touchPin();
                    delay(ttime);
                    utime = random(minTime2,maxTime2);   //取随机时间
                    unTouchPin();
                    delay(utime);    
                    count2--;
              }else{
                if(count3 >=0){
                    ttime = random(minTTime_3,maxTTime_3);   //取随机时间
                    touchPin();
                    delay(ttime);
                    utime = random(minTime3,maxTime3);   //取随机时间
                    unTouchPin();
                    delay(utime);    
                    count3--;
                }else{
                  count1 = COUNT_1;
                  count2 = COUNT_2;
                  count3 = COUNT_3;
                }
              }
          }       
  }else if(!k1 && k2 && k3){//1
     ttime = random(minTTime_1,maxTTime_1);   //取随机时间
      touchPin();
      delay(ttime);
      utime = random(minTime1,maxTime1);   //取随机时间
      unTouchPin();
      delay(utime);    
  }else if(!k2 && k1 && k3){ //2
      ttime = random(minTTime_2,maxTTime_2);   //取随机时间
      touchPin();
      delay(ttime);
      utime = random(minTime2,maxTime2);   //取随机时间
      unTouchPin();
      delay(utime);
  }else if(!k3 && k1 && k2){ //3
      ttime = random(minTTime_3,maxTTime_3);   //取随机时间
      touchPin();
      delay(ttime);
      utime = random(minTime3,maxTime3);   //取随机时间
      unTouchPin();
      delay(utime);        
  }else if(k1 && !k2 && !k3){ //频率2+3
          if(count2 >= 0){
             //红色
              ttime = random(minTTime_2,maxTTime_2);   //取随机时间
              touchPin();
              delay(ttime);
              utime = random(minTime2,maxTime2);   //取随机时间
              unTouchPin();
              delay(utime);   
              count2--;
          }else{
              if(count3 >=0){
                    ttime = random(minTTime_3,maxTTime_3);   //取随机时间
                    touchPin();
                    delay(ttime);
                    utime = random(minTime3,maxTime3);   //取随机时间
                    unTouchPin();
                    delay(utime);    
                    count3--;
              }else{
                count2 = COUNT_2;
                count3 = COUNT_3;
              }
          }     
  }
}

